package com.polaris.live.adapter.backpack

import android.graphics.Color
import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.R.drawable
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.databinding.ItemBackpackNumberBinding
import com.polaris.live.resp.back_resp.UserBackpackNumberResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import java.util.concurrent.TimeUnit

/**
 * BackPackNumberAdapter  靓号adapter
 *
 * @author Created by 半仙 on 2024/1/31/031 11:44
 */
class BackPackNumberAdapter : BaseBindingAdapter<ItemBackpackNumberBinding, UserBackpackNumberResp>() {

    private var mSelectIndex = -1

    init {
        addChildClickViewIds(R.id.tv_chick, R.id.tv_chick1)
    }

    override fun convert(
        holder: VBViewHolder<ItemBackpackNumberBinding>,
        item: UserBackpackNumberResp,
    ) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition
            if (position == mSelectIndex) {
                consTitle.setBackgroundResource(drawable.back_pack_select_back)
            } else {
                consTitle.setBackgroundResource(drawable.r8_back_ffffff)
            }
            tvName.setText("${item.userNumber}", true)
            tvName.setTextColor(Color.parseColor(item.color ?: "#000000"))
            Glide.with(icImage).load(ImageUtils.imgPath(item.numberMedalPng)).into(icImage)
            llTag.visibility = View.VISIBLE
            setTime(holder, item)
            if (item.useStatus.toBoolean()) {
                tvUsering.visibility = View.VISIBLE
                tvChick1.visibility = View.VISIBLE
                tvChick.visibility = View.GONE
            } else {
                tvChick1.visibility = View.GONE
                tvChick.visibility = View.VISIBLE
                tvUsering.visibility = View.GONE
            }
        }
    }

    override fun convert(
        holder: VBViewHolder<ItemBackpackNumberBinding>,
        item: UserBackpackNumberResp, data: List<Any>,
    ) {
        when (data.getOrNull(0)) {
            "notify" -> setTime(holder, item)
            "show" -> show(holder)
            "hide" -> hide(holder)
        }
    }

    fun show(holder: VBViewHolder<ItemBackpackNumberBinding>) {
        holder.vb.tvName.startAndResume()
    }

    fun hide(holder: VBViewHolder<ItemBackpackNumberBinding>) {
        holder.vb.tvName.pause()
    }

    private fun setTime(holder: VBViewHolder<ItemBackpackNumberBinding>, item: UserBackpackNumberResp) {
        holder.vb.apply {
            val expiredTime = item.expiredTime
            if (expiredTime.isNullOrEmpty()) {
                ivTimeIcon.visibility = View.GONE
                tvTime.text = StringUtils.getString(string.permanent)
            } else {
                ivTimeIcon.visibility = View.VISIBLE
                tvTime.text = formatTime(expiredTime.toLong() - CacheUtil.getServerTime())
            }
        }

    }

    private fun formatTime(timeInMillis: Long): String {
        val days = TimeUnit.MILLISECONDS.toDays(timeInMillis)
        val hours = TimeUnit.MILLISECONDS.toHours(timeInMillis)
        val minutes = TimeUnit.MILLISECONDS.toMinutes(timeInMillis)
        val seconds = TimeUnit.MILLISECONDS.toSeconds(timeInMillis)
        return when {
            days > 0 -> StringUtils.getString(string.days, days)
            hours > 0 -> StringUtils.getString(string.hours, hours)
            minutes > 0 -> StringUtils.getString(string.minutes, minutes)
            else -> StringUtils.getString(string.seconds, seconds)
        }
    }

    fun setSelect(position: Int) {
        notifyItemChanged(mSelectIndex)

        mSelectIndex = position

        notifyItemChanged(mSelectIndex)
    }

    fun getLastSelect() = mSelectIndex

    companion object {

        const val NOT_HAVE = -1L             //装扮id等于-1是未获取
    }
}